﻿using ArtMath.Core.Helper;
using ArtMath.Parameter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ArtMath.Data
{
    [Serializable]
    public class ManualParameterStringX : PadStringX
    {
        string paramReplacedExpression;
        public ManualParameterStringX(SketchPad pad) : base(pad)
        {

        }
        public ManualParameterStringX(SketchPad pad, string expression) : this(pad)
        {
            SetExpression(expression, false);
        }
        protected override bool CanCalc
        {
            get { return false; }
        }
        public void ClearParameterReplacedExpression()
        {
            paramReplacedExpression = null;
        }
        public double Calculate(IEnumerable<KeyValuePair<string, double>> values, bool tryUseParamReplacedExp = true)
        {
            string std;
            if (tryUseParamReplacedExp && paramReplacedExpression != null)
            {
                std = paramReplacedExpression;
            }
            else
            {
                std = StandardizedExpression;
                foreach (BasicParameter item in relatedParameters)
                {
                    std = Regex.Replace(std, $"\\b{item.Name}\\b", $"({item.Value.Value})");
                    //std = std.Replace(item.Name, item.Value.Value.ToString());
                }
                paramReplacedExpression = std;
            }
            foreach (KeyValuePair<string, double> item in values)
            {
                std = Regex.Replace(std, $"\\b{item.Key}\\b", $"({item.Value})");
            }
            Calculation.Calculate(std, out val);
            return Value;
        }
    }
}
